#! /usr/bin/python3
# -*- coding: UTF-8 -*-

if __name__ == "__main__":
	raise Exception("不支持从这里调用")

__version__ = "1.0"
__all__ = ["mousechsh_test"]

from com.mousechsh.common.data.json.MouseChshJson import mousechsh_json_to_json
from com.mousechsh.common.log.MouseChshLog import mousechsh_logging, mousechsh_logging_data
from com.mousechsh.common.util.MouseChshCodeUtil import mousechsh_code_util_method_rsa_2048, \
	mousechsh_code_util_type_utf8string, mousechsh_crypto_encrypt, \
	mousechsh_code_util_method_aes_128, mousechsh_code_util_mode_ecb, mousechsh_code_util_padding_pkcs5, \
	mousechsh_code_util_type_base64string, mousechsh_code_util_key_type_utf8string, mousechsh_crypto_sign, \
	mousechsh_code_util_key_format_hex_list, \
	mousechsh_code_util_key_format_asn1_der_base64, mousechsh_crypto_verify, mousechsh_code_util_padding_pkcs1, \
	mousechsh_code_util_hash_sha2_256
from com.mousechsh.common.middle.url.MouseChshUrl import MouseChshUrl


def mousechsh_test(*, use_assert=False):
	mousechsh_logging('开始测试【组合测试】【支付宝签名】相关代码：')

	# ----------------------------------------------------------------
	num = 1
	mousechsh_logging('开始执行测试【', num, '】……')
	keys = {
		'public':
			'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqzhulPvhM0dyeLFStL1S'
			'rJhu5TcPVUvSFKJAL0FnRISesSrb+NzB76LnDgF0R92z3jcGCDBzZ0JhJu0NOMRl'
			'hOTSkllW4ZBI2nIbrc4WGzZr9uVbqNjAPQ87c2fIB4TCDaXMyVLzKh9KCJvHJ0J6'
			'JLea2qGBlsR8sEd/tKB/BcZ0a0O/Io+8Koi8tPqNOVqrsV/hcC/4rXBmG4dRfn4M'
			'nZ/rh9bhCChlEyzpDn4J4SJkWadkwBgpf2l+KgHGgkC1ci3m7xEn5uK/7SBIi71B'
			'WKWdasKo6cWzudJSs+Ywkqy6B9BNXxKoAAPB59EhN4ZjXPb24s6gdnyWpjk8gdf0'
			'RQIDAQAB',
		'private':
			'MIIEpQIBAA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'
			'eNOc1G0NfwrVcms+mjDNhLFDh31I+Ib8P6iDCk0CgYEA7UrQ/PTS5SnFN/GpsXGE'
			'C3Calp6w0HEI4+wXVh6aShCgKB3ifxQTzDQlknYOHMJa3CnKyEt/j4HZf6s7ymOF'
			'ATKjh5CQ4hwAn4iEC9mn0xLwnlnSq36iBq9UKuRjkxHDo4GTaJ8n6Cx1PpWf9OdK'
			'jM8EG0qjF3XIHZhoAXH/E7kCgYEAlrI5IUYbaJg/8cN5fBYgv3YvS1yGZxKNXFY3'
			'F17OZH4Xv5/zJ9RsY+CQDpKl0wNlgOcI+O03utwu9OFOHZAQCcWvr7OebCYeTAML'
			'r7dKU3ZEi36Uxl/1rcOdanM5v39dCH9XOipsKw/NI1VrbIBVscpkdvRCtacZxHT7'
			'wDHLJDkCgYEAgR7TgUq4HK+jnixYq2JR78C9YnZB+9t1tzEb7tTTXloivPesd2ER'
			'fXOEwDJwHas3q00UOzt5+kLUNmYpgZXA4qT14w+Kt1+VzjwNHwDkRV+WgBLxkSiZ'
			'l/YM6kXoB8VVkPI9BVSGKntwNOP2o3Vmv6n4MbS0jCDVZPRoIh2EW1I='
	}
	mousechsh_logging_data(keys, '【支付宝签名】RSA密钥（2048位）为：')

	source = \
		'MouseChsh加密解密指纹签名测试文本：' \
		'1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' \
		'〇一二三四五六七八九十零壹贰叁肆伍陆柒捌玖拾佰仟万亿'
	mousechsh_logging_data(source, '【支付宝签名】原始数据为：')

	key = '1235678890ABCDEF'
	mousechsh_logging_data(key, '【支付宝签名】原始数据加密处理的密钥为：')

	base64_data = mousechsh_crypto_encrypt(
		source,
		public_key=key,
		method=mousechsh_code_util_method_aes_128,
		mode=mousechsh_code_util_mode_ecb,
		padding=mousechsh_code_util_padding_pkcs5,
		source_type=mousechsh_code_util_type_utf8string,
		target_type=mousechsh_code_util_type_base64string,
		key_type=mousechsh_code_util_key_type_utf8string,
		key_format=mousechsh_code_util_key_format_hex_list
	)
	mousechsh_logging_data(base64_data, '【支付宝签名】原始数据加密处理后的数据为：')

	url_obj = MouseChshUrl()
	url_obj.set_search('biz_content', base64_data)
	url_obj.set_search('timestamp', 1665294548)  # MouseChshDateTime().to_timestamp()
	url_obj.set_search('sign_type', 'RSA2')
	url_obj.set_search('charset', 'UTF-8')
	url_obj.set_search('format', 'JSON')
	url_string = url_obj.get_search(sort=True, trim=True, encode=True)
	mousechsh_logging_data(url_string, '【支付宝签名】要签名的数据为：')

	sign_string = mousechsh_crypto_sign(
		url_string,
		private_key=keys['private'],
		method=mousechsh_code_util_method_rsa_2048,
		hash_method=mousechsh_code_util_hash_sha2_256,
		mode=mousechsh_code_util_mode_ecb,
		padding=mousechsh_code_util_padding_pkcs1,
		source_type=mousechsh_code_util_type_utf8string,
		target_type=mousechsh_code_util_type_base64string,
		key_type=mousechsh_code_util_key_type_utf8string,
		key_format=mousechsh_code_util_key_format_asn1_der_base64
	)
	mousechsh_logging_data(sign_string, '【支付宝签名】签名结果为：')

	json_data = mousechsh_json_to_json({
		'data': base64_data,
		'sign': sign_string
	})
	mousechsh_logging_data(json_data, '【支付宝签名】最终组装的JSON数据为：')

	target_sign_string = \
		'kzbat6gGfb+JybogsS6lLh7G2OY7RJREgRzhfRoQKQk56AM2KQaPejHNOuGSnOOB' \
		'Qgw9+m5C+UGvv6LvyLlekfU76KKcvJ4gAgZihG3KonTAsaMB/bLFmmL21d9cdOVL' \
		'vKpyMixJHo/NGzK99nFy+mf3b8JDTL/ZCvMcy02j33zNYRTxU90g0dCC4cbm5YTB' \
		'0K76FfR4rSlMZQ4te5xSjpz40qtEb3BKmAoQrvT05pxpgPyiH5AvEzMNoymmPfVM' \
		'xIEW62LgERim0qpdj6uSWJjRR6TEkheNDbX0OVDOJkx4NZXosqdlI2leDzulk2nA' \
		'KiwGlPAuW9rXx8NRBlhvPA=='
	mousechsh_logging_data(target_sign_string, '【支付宝签名】一个已经签名的结果：')

	result = mousechsh_crypto_verify(
		url_string,
		target_sign_string,
		public_key=keys['public'],
		method=mousechsh_code_util_method_rsa_2048,
		hash_method=mousechsh_code_util_hash_sha2_256,
		mode=mousechsh_code_util_mode_ecb,
		padding=mousechsh_code_util_padding_pkcs1,
		source_type=mousechsh_code_util_type_utf8string,
		sign_type=mousechsh_code_util_type_base64string,
		key_type=mousechsh_code_util_key_type_utf8string,
		key_format=mousechsh_code_util_key_format_asn1_der_base64
	)
	mousechsh_logging_data(result, '【支付宝签名】对已经签名的结果进行验签的结果为：')
	if use_assert:
		assert result
